package com.hzcc.hzccblog.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzcc.hzccblog.common.Result;
import com.hzcc.hzccblog.common.ResultCodeEnum;
import com.hzcc.hzccblog.common.enums.AdminEnums;
import com.hzcc.hzccblog.common.enums.ArticleEnums;
import com.hzcc.hzccblog.common.enums.PublicEnums;
import com.hzcc.hzccblog.module.dao.AdminDao;
import com.hzcc.hzccblog.module.dao.ArticleDao;
import com.hzcc.hzccblog.module.entity.po.Admin;
import com.hzcc.hzccblog.module.entity.po.Article;
import com.hzcc.hzccblog.module.service.AdminService;
import com.hzcc.hzccblog.utils.FileUpdateUtil;
import com.hzcc.hzccblog.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.hzcc.hzccblog.common.Constant.*;

/**
 * @Author 早睡蛋
 * @Date 2022/6/20 16:15:10
 * @Desc:
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminDao, Admin> implements AdminService {

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private ArticleDao articleDao;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 获取头像
     * @return
     */
    @Override
    public Result getAvator() {
        String flag = redisUtil.getString(AVATOR_URL);
        if (flag!=null){
            return AdminEnums.AVATOR_URL.getResult().setData(flag);
        }
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*").eq("id","admin").eq("password","admin");
        Admin admin = adminDao.selectOne(queryWrapper);
        String avator = IMG_URL+admin.getAvator();
        redisUtil.setString(AVATOR_URL, avator);
        return AdminEnums.AVATOR_URL.getResult().setData(avator);
    }

    /**
     * 更新头像
     * @param file
     * @param myAvator
     * @return
     */
    @Override
    public Result updateAvator(MultipartFile file, String myAvator) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*").eq("id","admin").eq("password","admin");
        Admin admin = adminDao.selectOne(queryWrapper);
        String oldAvator = admin.getAvator().substring(10);
        FileUpdateUtil fileUpdateUtil = new FileUpdateUtil();
        Map<String,String> map = fileUpdateUtil.updateAvator(file,myAvator,oldAvator);
        String dataPath = map.get("dataPath");
        String fileUrl = map.get("fileUrl");
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id","admin");
        updateWrapper.set("avator",dataPath);
        int flag = adminDao.update(admin, updateWrapper);
        if (flag>0){
            redisUtil.delString(AVATOR_URL);
            return AdminEnums.AVATOR_URL.getResult().setData(fileUrl);
        }else {
            return PublicEnums.FAIL.getResult();
        }
    }

    /**
     * 获取文章数量
     * @return
     */
    @Override
    public Result articleCount() {
        String flag = redisUtil.getString(ARTICLE_COUNT);
        if (flag!=null){
            int flag1 = Integer.parseInt(flag);
            return AdminEnums.ARTICLE_COUNT.getResult().setData(flag1);
        }
        int count = articleDao.articleCount();
        redisUtil.setString(ARTICLE_COUNT, String.valueOf(count));
        return AdminEnums.ARTICLE_COUNT.getResult().setData(count);
    }


}
